let getRow = function (matrix, idx, num) {
    let ret = new Array();
    for (let index = 0; index < num; index++) {
        ret.push(matrix[idx * num + index]);
    }
    // console.log("getRow idx ", idx, " ret = ", ret)
    return ret;
}
let getCol = function (matrix, idx, num) {
    let ret = new Array();
    for (let index = 0; index < num; index++) {
        ret.push(matrix[index * num + idx]);
    }
    // console.log("getCol idx ", idx, " ret = ", ret)
    return ret;
}
let calculator = function (matrix1, matrix2) {
    let ret = new Array();
    let l = matrix2.length;
    let num = Math.sqrt(l);
    console.log('matrix1 = ', matrix1)
    console.log('matrix2 = ', matrix2)
    console.log("matrix: " + num + '*' + num)
    for (let index = 0; index < num; index++) {
        for (let index2 = 0; index2 < num; index2++) {
            let element = 0;
            let tmpRow = getRow(matrix1, index, num)
            let tmpCol = getCol(matrix2, index2, num)
            for (let index3 = 0; index3 < num; index3++) {
                element += tmpRow[index3] * tmpCol[index3];
            }
            ret.push(element);
        }
    }
    console.log('ret = ', ret)
}
calculator(new Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 1, 2, 3, 4), new Array(5, 6, 7, 8, 9, 1, 2, 3, 4, 1, 2, 3, 4, 5, 6, 7));

let calculator2 = function (m1, m2, colNum) {
    let ret = new Array();
    let l1 = m1.length;
    let rowNum = l1 / colNum;
    if (m2.length / colNum != Math.ceil(m2.length / colNum)) {
        console.error('m1 col num and m2 row num not equal')
        return;
    }
    for (let index = 0; index < rowNum; index++) {
        for (let index2 = 0; index2 < colNum; index2++) {
            let element = 0;
            let tmpRow = getRow(m1, index, colNum)
            let tmpCol = getCol(m2, index2, colNum)
            for (let index3 = 0; index3 < colNum; index3++) {
                element += tmpRow[index3] * tmpCol[index3];
            }
            ret.push(element);
        }
    }
    console.log('ret = ', ret)
}
console.log('calculator2 ')
calculator2(new Array(1, 2, 3, 4), new Array(5, 6, 7, 8, 9, 1, 2, 3, 4, 1, 2, 3, 4, 5, 6, 7), 4);

calculator2(new Array(1, 2, 3, 4, 5, 6, 7, 8), new Array(5, 6, 7, 8, 9, 1, 2, 3, 4, 1, 2, 3, 4, 5, 6, 7), 4);

calculator2(new Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 1, 2, 3, 4), new Array(5, 6, 7, 8, 9, 1, 2, 3, 4, 1, 2, 3, 4, 5, 6, 7), 4);